package com.Util;



import java.awt.Font;
import java.awt.RenderingHints;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.jfree.chart.*;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.util.Rotation;

/**
 * jfreeChart生成数据
 */
public class JfreeChartUtil {
    public static void main(String[] args) {
    /*    // 1. 得到数据
        CategoryDataset dataset = getDataSet();
        // 2. 构造chart
        JFreeChart chart = ChartFactory.createBarChart3D(
                "水果产量图", // 图表标题
                "水果", // 目录轴的显示标签--横轴
                "产量", // 数值轴的显示标签--纵轴
                dataset, // 数据集
                PlotOrientation.VERTICAL, // 图表方向：水平、
                true, // 是否显示图例(对于简单的柱状图必须
                false, // 是否生成工具
                false // 是否生成URL链接
        );
        // 3. 处理chart中文显示问题
        processChart(chart);

        // 4. chart输出图片
        writeChartAsImage(chart);



        // 5. chart 以swing形式输出
        ChartFrame pieFrame = new ChartFrame("军哥数据分析", chart);
        pieFrame.pack();
        pieFrame.setVisible(true);*/
       //饼状图
        createPieChart();
        //3D饼状图
        createPieChart3D();
        //柱状图
        createColumnChart();
        //折线图
        createLineChart();
        //3D折线图
        createLineChart3D();


    }

    /**
     * 获取一个演示用的组合数据集对象
     *
     * @return
     */
    private static CategoryDataset getDataSet() {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset.addValue(100, "北京", "苹果");
        dataset.addValue(120, "上海", "苹果");
        dataset.addValue(160, "广州", "苹果");
        dataset.addValue(210, "北京", "梨子");
        dataset.addValue(220, "上海", "梨子");
        dataset.addValue(230, "广州", "梨子");
        dataset.addValue(330, "北京", "葡萄");
        dataset.addValue(340, "上海", "葡萄");
        dataset.addValue(340, "广州", "葡萄");
        dataset.addValue(420, "北京", "香蕉");
        dataset.addValue(430, "上海", "香蕉");
        dataset.addValue(400, "广州", "香蕉");
        dataset.addValue(510, "北京", "荔枝");
        dataset.addValue(530, "上海", "荔枝");
        dataset.addValue(510, "广州", "荔枝");
        return dataset;
    }

    /**
     * 解决图表汉字显示问题
     *
     * @param chart
     */
    private static void processChart(JFreeChart chart) {
        CategoryPlot plot = chart.getCategoryPlot();
        CategoryAxis domainAxis = plot.getDomainAxis();
        ValueAxis rAxis = plot.getRangeAxis();
        chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,
                RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
        TextTitle textTitle = chart.getTitle();
        textTitle.setFont(new Font("宋体", Font.PLAIN, 20));
        domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));
        domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));
        rAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));
        rAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));
        chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));
        // renderer.setItemLabelGenerator(new LabelGenerator(0.0));
        // renderer.setItemLabelFont(new Font("宋体", Font.PLAIN, 12));
        // renderer.setItemLabelsVisible(true);
    }

    /**
     * 输出图片
     * 树状图
     * @param chart
     */
    private static void writeChartAsImage(JFreeChart chart) {
        FileOutputStream fos_jpg = null;
        try {
            fos_jpg = new FileOutputStream("D:\\test\\fruit.jpg");
            //吃出上传至
            ChartUtilities.writeChartAsJPEG(fos_jpg, 1, chart, 400, 300, null);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fos_jpg.close();
            } catch (Exception e) {
            }
        }
    }

    /**
     * 饼状图
     */

    public static JFreeChart createPieChart() {
        DefaultPieDataset dataset = new DefaultPieDataset();
        dataset.setValue("苹果", 250);
        dataset.setValue("桔子", 350);
        dataset.setValue("梨子", 200);
        dataset.setValue("香蕉", 50);
        dataset.setValue("荔枝", 150);
        //创建主题样式
        StandardChartTheme standardChartTheme=new StandardChartTheme("CN");
        //设置标题字体
        standardChartTheme.setExtraLargeFont(new Font("隶书",Font.BOLD,20));
        //设置图例的字体
        standardChartTheme.setRegularFont(new Font("宋书",Font.PLAIN,15));
        //设置轴向的字体
        standardChartTheme.setLargeFont(new Font("宋书",Font.PLAIN,15));
        //应用主题样式
        ChartFactory.setChartTheme(standardChartTheme);

        JFreeChart chart = ChartFactory.createPieChart("水果产量比率图", dataset,
                true, true, true);

        Plot cp = chart.getPlot();
        cp.setBackgroundPaint( ChartColor.WHITE); // 背景色设置

        // ChartFrame frame = new ChartFrame("水果产量比率图 ", chart, true);
        // frame.pack();
        // frame.setVisible(true);

        try {
            ChartUtilities.saveChartAsPNG(new File("D:\\test\\PieChart.png"),
                    chart, 800, 500);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return chart;
    }

    /**
     * 3D饼状图
     */
    public static void createPieChart3D() {
        DefaultPieDataset dataset = new DefaultPieDataset();
        dataset.setValue("苹果", 250);
        dataset.setValue("桔子", 350);
        dataset.setValue("梨子", 200);
        dataset.setValue("香蕉", 50);
        dataset.setValue("荔枝", 150);
        //创建主题样式
        StandardChartTheme standardChartTheme=new StandardChartTheme("CN");
        //设置标题字体
        standardChartTheme.setExtraLargeFont(new Font("隶书",Font.BOLD,20));
        //设置图例的字体
        standardChartTheme.setRegularFont(new Font("宋书",Font.PLAIN,15));
        //设置轴向的字体
        standardChartTheme.setLargeFont(new Font("宋书",Font.PLAIN,15));
        //应用主题样式
        ChartFactory.setChartTheme(standardChartTheme);

        JFreeChart chart = ChartFactory.createPieChart3D("水果产量比率图", dataset,
                true, true, true);
        PiePlot3D plot = (PiePlot3D) chart.getPlot();
        // 设置开始角度
        plot.setStartAngle(150D);
        // 设置方向为"顺时针方向"
        plot.setDirection( Rotation.CLOCKWISE);
        // 设置透明度，0.5F为半透明，1为不透明，0为全透明
        plot.setForegroundAlpha(1F);
        // 没有数据的时候显示的内容
        plot.setNoDataMessage("无数据显示");
        // 背景色设置
        plot.setBackgroundPaint(ChartColor.WHITE);
        // ChartFrame frame = new ChartFrame("水果产量比率图 ", chart, true);
        // frame.pack();
        // frame.setVisible(true);

        try {
            ChartUtilities.saveChartAsPNG(new File("D:\\test\\PieChart3D.png"),
                    chart, 800, 500);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 柱状图
     */
    public static void createColumnChart() {

        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset.addValue(100, "北京", "苹果");
        dataset.addValue(100, "上海", "苹果");
        dataset.addValue(100, "广州", "苹果");
        dataset.addValue(200, "北京", "梨子");
        dataset.addValue(200, "上海", "梨子");
        dataset.addValue(200, "广州", "梨子");
        dataset.addValue(300, "北京", "葡萄");
        dataset.addValue(300, "上海", "葡萄");
        dataset.addValue(300, "广州", "葡萄");
        dataset.addValue(400, "北京", "香蕉");
        dataset.addValue(400, "上海", "香蕉");
        dataset.addValue(400, "广州", "香蕉");
        dataset.addValue(500, "北京", "荔枝");
        dataset.addValue(500, "上海", "荔枝");
        dataset.addValue(500, "广州", "荔枝");

        //创建主题样式
        StandardChartTheme standardChartTheme=new StandardChartTheme("CN");
        //设置标题字体
        standardChartTheme.setExtraLargeFont(new Font("隶书",Font.BOLD,20));
        //设置图例的字体
        standardChartTheme.setRegularFont(new Font("宋书",Font.PLAIN,15));
        //设置轴向的字体
        standardChartTheme.setLargeFont(new Font("宋书",Font.PLAIN,15));

        //应用主题样式
        ChartFactory.setChartTheme(standardChartTheme);


        JFreeChart chart = ChartFactory.createBarChart("水果产量图", "水量", "产量",
                dataset, PlotOrientation.VERTICAL, true, true, true);




        CategoryPlot cp = chart.getCategoryPlot();
        cp.setBackgroundPaint(ChartColor.WHITE); // 背景色设置
        cp.setRangeGridlinePaint(ChartColor.GRAY); // 网格线色设置

        // ChartFrame frame=new ChartFrame ("水果产量图 ",chart,true);
        // frame.pack();
        // frame.setVisible(true);

        try {
            ChartUtilities.saveChartAsPNG(new File("D:\\test\\ColumnChart.png"),
                    chart, 800, 500);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    /**
     * 折线图
     */
    public static void createLineChart() {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset.addValue(100, "北京", "苹果");
        dataset.addValue(200, "上海", "苹果");
        dataset.addValue(300, "广州", "苹果");
        dataset.addValue(400, "北京", "梨子");
        dataset.addValue(500, "上海", "梨子");
        dataset.addValue(600, "广州", "梨子");
        dataset.addValue(700, "北京", "葡萄");
        dataset.addValue(800, "上海", "葡萄");
        dataset.addValue(900, "广州", "葡萄");
        dataset.addValue(800, "北京", "香蕉");
        dataset.addValue(700, "上海", "香蕉");
        dataset.addValue(600, "广州", "香蕉");
        dataset.addValue(500, "北京", "荔枝");
        dataset.addValue(400, "上海", "荔枝");
        dataset.addValue(300, "广州", "荔枝");

        //创建主题样式
        StandardChartTheme standardChartTheme=new StandardChartTheme("CN");
        //设置标题字体
        standardChartTheme.setExtraLargeFont(new Font("隶书",Font.BOLD,20));
        //设置图例的字体
        standardChartTheme.setRegularFont(new Font("宋书",Font.PLAIN,15));
        //设置轴向的字体
        standardChartTheme.setLargeFont(new Font("宋书",Font.PLAIN,15));
        //应用主题样式
        ChartFactory.setChartTheme(standardChartTheme);

        JFreeChart chart = ChartFactory.createLineChart("水果产量图 ", "水果", "产量",
                dataset, PlotOrientation.VERTICAL, true, true, true);

        CategoryPlot cp = chart.getCategoryPlot();
        cp.setBackgroundPaint(ChartColor.WHITE); // 背景色设置
        cp.setRangeGridlinePaint(ChartColor.GRAY); // 网格线色设置

        // ChartFrame frame=new ChartFrame ("水果产量图 ",chart,true);
        // frame.pack();
        // frame.setVisible(true);

        try {
            ChartUtilities.saveChartAsPNG(new File("D:\\test\\LineChart.png"),
                    chart, 800, 500);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 3D折线图
     */
    public static void createLineChart3D() {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset.addValue(300, "北京", "苹果");
        dataset.addValue(100, "上海", "苹果");
        dataset.addValue(900, "广州", "苹果");
        dataset.addValue(200, "北京", "梨子");
        dataset.addValue(200, "上海", "梨子");
        dataset.addValue(700, "广州", "梨子");
        dataset.addValue(300, "北京", "葡萄");
        dataset.addValue(300, "上海", "葡萄");
        dataset.addValue(300, "广州", "葡萄");
        dataset.addValue(400, "北京", "香蕉");
        dataset.addValue(800, "上海", "香蕉");
        dataset.addValue(400, "广州", "香蕉");
        dataset.addValue(100, "北京", "荔枝");
        dataset.addValue(500, "上海", "荔枝");
        dataset.addValue(500, "广州", "荔枝");

        //创建主题样式
        StandardChartTheme standardChartTheme=new StandardChartTheme("CN");
        //设置标题字体
        standardChartTheme.setExtraLargeFont(new Font("隶书",Font.BOLD,20));
        //设置图例的字体
        standardChartTheme.setRegularFont(new Font("宋书",Font.PLAIN,15));
        //设置轴向的字体
        standardChartTheme.setLargeFont(new Font("宋书",Font.PLAIN,15));
        //应用主题样式
        ChartFactory.setChartTheme(standardChartTheme);

        JFreeChart chart = ChartFactory.createLineChart3D("水果产量图 ", "水果", "产量",
                dataset, PlotOrientation.VERTICAL, true, true, true);

        CategoryPlot cp = chart.getCategoryPlot();
        cp.setBackgroundPaint(ChartColor.WHITE); // 背景色设置
        cp.setRangeGridlinePaint(ChartColor.GRAY); // 网格线色设置

        // ChartFrame frame=new ChartFrame ("水果产量图 ",chart,true);
        // frame.pack();
        // frame.setVisible(true);

        try {
            ChartUtilities.saveChartAsPNG(new File("D:\\test\\LineChart3D.png"),
                    chart, 800, 500);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
